package com.linkage.service.background;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.linkage.pojo.background.UserRole;
import com.linkage.pojo.consumer.User;
import com.linkage.repository.background.UserManageRepository;

/**
 * @标题：用户后台管理业务
 * @描述：用户业务操作
 * @author: 邓达
 */

@Service
public class UserManageService {

    @Autowired
    private UserManageRepository userManageRepository;

    // FIXME
    public boolean createUser(User user, String roleName) {

        if (this.userManageRepository.getOneUserById(user.getUserId()) != null
                || this.userManageRepository.getOneUserByLoginName(user.getLoginName()) != null
                || StringUtils.isEmpty(user.getPassword())) {
            return false;
        }
        Date now = new Date();
        user.setRegisterTime(now);

        if ("普通用户".equals(roleName) || "订单管理员".equals(roleName) || "商品管理员".equals(roleName) || "超级管理员".equals(roleName)
                || "用户管理员".equals(roleName)) {
            this.userManageRepository.saveUser(user);
            UserRole userRole = new UserRole();
            userRole.setRoleName(roleName);
            userRole.setUserId(user.getUserId());
            this.saveUserRole(userRole);

            return true;
        }

        return false;
    }

    public List<Map<String, Object>> listAllUsers() {
        return userManageRepository.listAllUsers();
    }

    public void saveUser(User user) {
        this.userManageRepository.saveUser(user);
    }
    
    //封禁与解封用户
    public void changeUserStatus(@Param("userId") Integer userId,@Param("userStatus") String userStatus){
        
        if(this.userManageRepository.getOneUserById(userId) != null){
            if(this.userManageRepository.findUserRoleById(userId) == null){
                UserRole userRole=new UserRole();
                userRole.setUserId(userId);
               this.userManageRepository.saveUserRole(userRole);
            }
            this.userManageRepository.changeUserStatus(userId, userStatus);
        }
    
    }

    public boolean editUser(User user, String changeRoleName) {


        if ( StringUtils.isEmpty(user.getLoginName())
                || user.getLoginName() == null
                || "".equals(user.getLoginName())
                || StringUtils.isEmpty(user.getPassword())) {
           return false;
        }

            this.update(user);
            UserRole userRole = this.findUserRoleById(user.getUserId());
            if (userRole != null) {
                userRole.setRoleName(changeRoleName);
                this.modifyUserRole(userRole);
            } else {
                this.addPermission(user.getUserId(), changeRoleName);
            }
            return true;
    }

    public void update(User user) {
        this.userManageRepository.update(user);
    }

    public void deleteUser(Integer userId) {
        userManageRepository.deleteUser(userId);
        UserRole userRole = this.userManageRepository.findUserRoleById(userId);
        userManageRepository.deleteUserRole(userRole);
    }

    public User getOneUserById(Integer userId) {
        return userManageRepository.getOneUserById(userId);
    }

    public User getOneUserByLoginName(String loginName) {
        return userManageRepository.getOneUserByLoginName(loginName);
    }

    // 添加权限
    public boolean addPermission(@Param("userId") Integer userId, @Param("roleName") String roleName) {

        if (this.userManageRepository.getOneUserById(userId) != null) {
            UserRole userRole = new UserRole();
            userRole.setRoleName(roleName);
            userRole.setUserId(userId);
            this.userManageRepository.saveUserRole(userRole);
            return true;
        }
        return false;

    }

    // 查找用户角色名称
    public UserRole findUserRoleById(@Param("userId") Integer userId) {

        return this.userManageRepository.findUserRoleById(userId);
    }

    // 修改用户角色名称
    public void modifyUserRole(UserRole userRole) {
        this.userManageRepository.modifyUserRole(userRole);

    }

    public void saveUserRole(UserRole userRole) {
        this.userManageRepository.saveUserRole(userRole);

    }

    public void deleteUserRole(UserRole userRole) {
        this.userManageRepository.deleteUserRole(userRole);
    }
    
    

}
